import { Component, Injector, ViewEncapsulation } from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  PipelineListDto,
  RequirementMaterialListDto,
} from "@shared/service-proxies/service-proxies";
import { PipelineNodeServiceProxy } from "@shared/service-proxies/service-proxies";
import { finalize } from "rxjs/operators";
import { appModuleAnimation } from "@shared/animations/routerTransition";

@Component({
  selector: "yl13",
  templateUrl: "./yl13.component.html",
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()],
})
export class YL13ModalComponent extends AppComponentBase {
  saving = false;

  pipe: PipelineListDto = new PipelineListDto();

  remark = "";

  bills = new Array<RequirementMaterialListDto>();
  subTaskCode = "";
  drawingCode = "";

  constructor(
    injector: Injector,
    private _taskServiceProxy: PipelineNodeServiceProxy
  ) {
    super(injector);
  }

  load(pipe: PipelineListDto): void {
    this.pipe = pipe;

    // this._taskServiceProxy.getCurrentActRemark(pipe.id).subscribe(result => {
    //   this.remark = typeof result === 'object' ? '' : result;
    // });

    this._taskServiceProxy
      .getRequirementBillByTask(pipe.id)
      .subscribe((result) => {
        this.bills = result.requirementMaterials;
        this.subTaskCode = result.subTaskCode;
        this.drawingCode = result.drawingCode;
      });
  }

  save(): void {
    this.saving = true;

    this._taskServiceProxy
      .setCurrentActRemark(this.pipe.id, this.remark)
      .pipe(
        finalize(() => {
          this.saving = false;
        })
      )
      .subscribe(result => {
        this.notify.success('记录备注成功');
      });
  }
}
